/**
 * <p>Title: EventListenerHandler.java</p>
 * <p>Description: </p>
 * <p>Company: www.huoyunren.com</p>
 * @author duanxiangyun
 * @date 2015年5月13日
 * @version 1.0
 */
package com.hxbd.test.handles;

import java.util.Calendar;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.ScreenshotException;
import org.openqa.selenium.support.events.WebDriverEventListener;

import com.duan.test.utils.common.CommonUtils;
import com.hxbd.test.utils.webdriver.EleOperationUtil;
import com.hxbd.test.utils.webdriver.FindElementUtil;


/**
 * 事件监听类：暂时没用上：为以后扩展保存，可能会用于web中异常监听处理
 */
public class EventListenerHandler implements WebDriverEventListener {
	@Override
	public void onException(Throwable ex, WebDriver arg1) {
		// String msg = ex.getClass().toString();
		// if (msg != null &&
		// !msg.contains("org.openqa.selenium.NoSuchElementException")) {
		// String []exc = msg.split("\\.");//.必须转义
		// Assert.fail("抛出异常：" + exc[exc.length - 1].replaceAll("Exception",
		// ""));
		// }
		// String filename = generateFilename(ex,filepath,testClass,tessMethod);
		// File srcFile =
		// ((TakesScreenshot)arg1).getScreenshotAs(OutputType.FILE);
		// try {
		// FileUtils.copyFile(srcFile,new File(filename));
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// try {
		// byte[] btDataFile =
		// Base64.decodeBase64(extractScreenShot(ex).getBytes());
		// File of = new File(filename);
		// FileOutputStream osf = new FileOutputStream(of);
		// osf.write(btDataFile);
		// osf.flush();
		// osf.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

	}

	/**
	 * 获得文件
	 * 
	 * @param ex
	 *            异常
	 * @param filepath
	 *            文件路径
	 * @param testClass
	 *            测试类
	 * @param tessMethod
	 *            测试方法
	 * @return 文件相对路劲+处理过的文件名
	 */
	private String generateFilename(Throwable ex, String filepath,
			String testClass, String tessMethod) {
		Calendar c = Calendar.getInstance();
		String filename = ex.getMessage();
		int i = filename.indexOf('\n');
		filename = filename.substring(0, i).replaceAll("\\s", "_")
				.replaceAll(":", "")
				+ ".png";
		filename = CommonUtils.getFilePath(filepath) + "\\" + testClass + "_"
				+ tessMethod + c.get(Calendar.YEAR) + "-"
				+ c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH)
				+ "-" + c.get(Calendar.HOUR_OF_DAY) + "-"
				+ c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND) + "-"
				+ filename;
		return filename;
	}

	/**
	 * 根据异常截图
	 * 
	 * @param ex
	 *            异常
	 * @return 截图
	 */
	private String extractScreenShot(Throwable ex) {
		Throwable cause = ex.getCause();
		if (cause instanceof ScreenshotException) {
			return ((ScreenshotException) cause).getBase64EncodedScreenshot();
		}
		return null;
	}

	public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#afterClickOn
	 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)
	 */
	@Override
	public void afterClickOn(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#afterFindBy
	 * (org.openqa.selenium.By, org.openqa.selenium.WebElement,
	 * org.openqa.selenium.WebDriver)
	 */
	@Override
	public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
		// TODO Auto-generated method stub
		// try {
		// arg1 =
		// arg2.findElement(By.xpath(".//*[@id='content']/div/div/div/div/div/h1"));
		// }catch (NoSuchElementException e) {
		// arg1 = null;
		// }
		// finally {
		// if (arg1 != null) {
		// if (arg1.getText().contains("500") || arg1.getText().contains("404")
		// || arg1.getText().contains("403")) {
		// Assert.fail("Http请求异常");
		// }
		// }
		// }
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateBack
	 * (org.openqa.selenium.WebDriver)
	 */
	@Override
	public void afterNavigateBack(WebDriver arg0) {
		// TODO Auto-generated method stub
		FindElementUtil.waitForPageLoad(arg0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#
	 * afterNavigateForward(org.openqa.selenium.WebDriver)
	 */
	@Override
	public void afterNavigateForward(WebDriver arg0) {
		// TODO Auto-generated method stub
		FindElementUtil.waitForPageLoad(arg0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateTo
	 * (java.lang.String, org.openqa.selenium.WebDriver)
	 */
	@Override
	public void afterNavigateTo(String arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		FindElementUtil.waitForPageLoad(arg1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#afterScript
	 * (java.lang.String, org.openqa.selenium.WebDriver)
	 */
	@Override
	public void afterScript(String arg0, WebDriver arg1) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#beforeChangeValueOf
	 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)
	 */
	public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#beforeClickOn
	 * (org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver)
	 */
	@Override
	public void beforeClickOn(WebElement arg0, WebDriver arg1) {
		// TODO Auto-generated method stub
		EleOperationUtil.waitElementClickable(arg1, arg0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#beforeFindBy
	 * (org.openqa.selenium.By, org.openqa.selenium.WebElement,
	 * org.openqa.selenium.WebDriver)
	 */
	@Override
	public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateBack
	 * (org.openqa.selenium.WebDriver)
	 */
	@Override
	public void beforeNavigateBack(WebDriver arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#
	 * beforeNavigateForward(org.openqa.selenium.WebDriver)
	 */
	@Override
	public void beforeNavigateForward(WebDriver arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateTo
	 * (java.lang.String, org.openqa.selenium.WebDriver)
	 */
	@Override
	public void beforeNavigateTo(String arg0, WebDriver arg1) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.openqa.selenium.support.events.WebDriverEventListener#beforeScript
	 * (java.lang.String, org.openqa.selenium.WebDriver)
	 */
	@Override
	public void beforeScript(String arg0, WebDriver arg1) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#afterAlertAccept(org.openqa.selenium.WebDriver)
	 */
	@Override
	public void afterAlertAccept(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#afterAlertDismiss(org.openqa.selenium.WebDriver)
	 */
	@Override
	public void afterAlertDismiss(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#afterChangeValueOf(org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver, java.lang.CharSequence[])
	 */
	@Override
	public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#afterNavigateRefresh(org.openqa.selenium.WebDriver)
	 */
	@Override
	public void afterNavigateRefresh(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#beforeAlertAccept(org.openqa.selenium.WebDriver)
	 */
	@Override
	public void beforeAlertAccept(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#beforeAlertDismiss(org.openqa.selenium.WebDriver)
	 */
	@Override
	public void beforeAlertDismiss(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#beforeChangeValueOf(org.openqa.selenium.WebElement, org.openqa.selenium.WebDriver, java.lang.CharSequence[])
	 */
	@Override
	public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.openqa.selenium.support.events.WebDriverEventListener#beforeNavigateRefresh(org.openqa.selenium.WebDriver)
	 */
	@Override
	public void beforeNavigateRefresh(WebDriver arg0) {
		// TODO Auto-generated method stub
		
	}
}
